package com.lijy.employee.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.lijy.employee.domain.Department;
import com.lijy.employee.domain.Employee;
import com.lijy.employee.domain.PageBean;
import com.lijy.employee.service.DepartmentService;
import com.lijy.employee.service.EmployeeService;

@Controller
@SessionAttributes({"employee","ename"})
@RequestMapping("/employee")
public class EmployeeController {

	private static Logger log = LoggerFactory.getLogger(EmployeeController.class);
	
	@Autowired
	private EmployeeService employeeService;
	
	@Autowired
	private DepartmentService departmentService;
	
	Map<String, Object> employeeMap = new HashMap<String, Object>();

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(Model model) {
		// 开启modelDriven
		model.addAttribute(new Employee());
		return "index";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(Employee employee, ModelMap model) {
		Employee e = employeeService.findByUsernameAndPassword(employee);
		if (e == null) {
			return "redirect:login";
		}
		// employee会自己注入session中
		// 将ename放入session作用域中，这样转发页面也可以取到这个数据
		model.addAttribute("ename", e.getEname());
		return "frame";
	}
	
	@RequestMapping(value = "/frame", method = RequestMethod.GET)
	public String frame() {
		return "frame";
	}
	
    @RequestMapping(value="/logout", method=RequestMethod.GET)
	public String logout (String ename, Model model) {
		return "index";
	}
    
	@RequestMapping(value="/list", method=RequestMethod.GET)
    public String list(@RequestParam(value="currentPage",defaultValue="1",required=false)int currentPage, Model model) {
		PageBean<Employee> pageBean = employeeService.findByPage(currentPage);
		model.addAttribute("pageBean", pageBean);
    	return "/frame/listEmployee";
    }
    
	@RequestMapping(value="/edit/{eid}", method=RequestMethod.GET)
	public String edit(@PathVariable("eid") String eid, Map<String, Object> model) {
		Employee employee = employeeService.findById(eid);
		log.info("edit employee = " + employee);
		List<Department> departments = departmentService.findAll();
		log.info("list department = " + departments );
		model.put("employee", employee);
		model.put("departments", departments);
		return "/frame/editEmployee";
	}
	
	@RequestMapping(value="/update", method = RequestMethod.POST)
	public String update(@ModelAttribute Employee employee) {
		employeeService.update(employee);
		return "redirect:list";
	}
	
	@RequestMapping(value="/delete/{eid}", method=RequestMethod.GET)
	public String delete(@PathVariable("eid") String eid) {
		employeeService.delete(eid);
		return "redirect:/employee/list";
	}
	
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String addEmployee(Model model) {
		List<Department> list = departmentService.findAll();
		model.addAttribute("departmentList", list);
		return "/frame/addEmployee";
	}
	
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public String save(Employee employee) {
		employeeService.save(employee);
		return "redirect:/employee/list";
	}
	
	@InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
	
}
